<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">

    <h:body>
        <ui:composition template="/resources/template/template.xhtml">
            <ui:define name="content">
                <h:form>
                    <p:panel header="Search Bills" >
                        <f:facet name="header">
                            <h:panelGrid columns="1" >
                                <h:panelGrid columns="7" >

                                    <h:outputLabel value="From Date"/>
                                    <p:calendar styleClass="dateTimePicker" id="fromDate" value="#{searchController.fromDate}" navigator="false" pattern="dd MMMM yyyy hh mm a" >      
                                    </p:calendar>
                                    <h:outputLabel value="To Date"/>
                                    <p:calendar id="toDate" value="#{searchController.toDate}" navigator="false" pattern="dd MMMM yyyy hh mm a" >                                                                              
                                    </p:calendar>
                                    <p:commandButton id="btnSearch3" ajax="false" value="Search Loged Department" action="#{searchController.createPatientInvestigationsTableLogin()}" onclick="onSubmitButton();" />
                                    <p:commandButton id="btnSearch" ajax="false" value="Search" action="#{searchController.createPatientInvestigationsTable()}" onclick="onSubmitButton();" />
                                    <p:commandButton id="btnSearch2" ajax="false" value="Search All" action="#{searchController.createPatientInvestigationsTableAll()}" onclick="onSubmitButton();" />
                                </h:panelGrid>
                                <h:panelGrid columns="4" >
                                    <h:outputLabel value="Bill No"/>
                                    <h:outputLabel value="Patient Name"/>
                                    <h:outputLabel value="Patient Phone"/>                               
                                    <h:outputLabel value="Investigation Name"/>
                                    <p:inputText autocomplete="off"  value="#{searchController.searchKeyword.billNo}" />
                                    <p:inputText autocomplete="off" value="#{searchController.searchKeyword.patientName}" />
                                    <p:inputText autocomplete="off"  value="#{searchController.searchKeyword.patientPhone}" />
                                    <p:inputText autocomplete="off" value="#{searchController.searchKeyword.itemName}"/>
                                    <h:outputLabel value="Report Department"/>
                                    <h:outputLabel value="Bill Department"/>
                                    <h:outputLabel value="BHT"/>
                                    <h:outputLabel/>
                                    <p:autoComplete completeMethod="#{departmentController.completeDept}" styleClass="shortinput2"
                                                    var="dept" itemLabel="#{dept.name}" itemValue="#{dept}" forceSelection="true" 
                                                    value="#{searchController.reportKeyWord.department}" converter="departmentConverter"/>
                                    <p:autoComplete completeMethod="#{departmentController.completeDept}" styleClass="shortinput2"
                                                    var="dept" itemLabel="#{dept.name}" itemValue="#{dept}" forceSelection="true" 
                                                    value="#{searchController.reportKeyWord.departmentFrom}" converter="departmentConverter"/>
                                    <p:autoComplete converter="admis" widgetVar="aPt" id="acPt" forceSelection="true" styleClass="shortinput2"
                                                    value="#{searchController.patientEncounter}"
                                                    completeMethod="#{admissionController.completePatientAll}" 
                                                    var="myItem" itemValue="#{myItem}" 
                                                    itemLabel="#{myItem.bhtNo}" 
                                                    size="30"  >
                                        <p:column>
                                            <p:outputLabel value="#{myItem.bhtNo}" />
                                        </p:column>
                                        <p:column>
                                            <p:outputLabel value="#{myItem.patient.person.nameWithTitle}" />

                                        </p:column>
                                        <p:column>
                                            <h:outputLabel value="#{myItem.currentPatientRoom.roomFacilityCharge.name}"/>
                                        </p:column>
                                        <p:column>
                                            <h:outputLabel value="Discharged"  rendered="#{myItem.discharged}"/>
                                        </p:column>
                                        <p:column>
                                            <h:outputLabel value="Discharged"  rendered="#{myItem.paymentFinalized}"/>
                                        </p:column>
                                        <p:column>
                                            <h:outputLabel  value="Cancelled"  rendered="#{myItem.retired}"/>
                                        </p:column>
                                    </p:autoComplete>

                                </h:panelGrid>
                            </h:panelGrid>
                        </f:facet>




                        <p:dataTable  id="tblBills" value="#{searchController.patientInvestigations}" var="pi" rowIndexVar="i" >
                            <p:column  headerText="Bill" styleClass="alignTop" >
                                <h:outputLabel id="lblBillNo" 
                                               value="#{pi.billItem.bill.insId}" 
                                               styleClass="#{pi.billItem.bill.cancelled or pi.billItem.transRefund ? 'redtext':''}"></h:outputLabel>
                                <p:tooltip for="lblBillNo"  >
                                    <h:outputLabel value="Bill No : #{pi.billItem.bill.insId}" ></h:outputLabel>
                                    <br/>
                                    <h:outputLabel value="Billed From : #{pi.billItem.bill.institution.name}" ></h:outputLabel>
                                    <br/>
                                    <h:outputLabel value="Sampled : " />
                                    <h:outputLabel rendered="#{pi.collected}" value="#{pi.sampledAt}" >
                                        <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                    </h:outputLabel>
                                    <br/>
                                    <h:outputLabel value="Received : " />
                                    <h:outputLabel rendered="#{pi.received}" value="#{pi.receivedAt}" >
                                        <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                    </h:outputLabel>
                                    <br/>
                                    <h:panelGroup rendered="#{pi.billItem.bill.cancelled}" >
                                        <h:outputLabel style="color: red;" value="Cancelled : " />
                                        <h:outputLabel style="color: red;" rendered="#{pi.billItem.bill.cancelled}" value="#{pi.billItem.bill.cancelledBill.createdAt}" >
                                            <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                        </h:outputLabel>
                                    </h:panelGroup>
                                    <h:panelGroup rendered="#{pi.billItem.refunded}" >
                                        <h:outputLabel style="color: red;" value="Refunded : " />
                                        <h:outputLabel style="color: red;" rendered="#{pi.billItem.refunded}" value="#{pi.billItem.referanceBillItem.bill.createdAt}" >
                                            <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                        </h:outputLabel>
                                    </h:panelGroup>


                                </p:tooltip>
                            </p:column>

                            <p:column  headerText="Dep ID" styleClass="alignTop" rendered="false" >
                                <h:outputLabel id="lblInvNo" 
                                               value="#{pi.billItem.bill.deptId}" 
                                               styleClass="#{pi.billItem.bill.cancelled or pi.billItem.transRefund ? 'redtext':''}"></h:outputLabel>
                                <p:tooltip for="lblBillNo"  >
                                    <h:outputLabel value="Bill No : #{pi.billItem.bill.deptId}" ></h:outputLabel>
                                    <br/>
                                    <h:outputLabel value="Billed From : #{pi.billItem.bill.institution.name}" ></h:outputLabel>
                                    <br/>
                                    <h:outputLabel value="Sampled : " />
                                    <h:outputLabel rendered="#{pi.collected}" value="#{pi.sampledAt}" >
                                        <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm:ss a"/>
                                    </h:outputLabel>
                                    <br/>
                                    <h:outputLabel value="Received : " />
                                    <h:outputLabel rendered="#{pi.received}" value="#{pi.receivedAt}" >
                                        <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm:ss a"/>
                                    </h:outputLabel>
                                    <br/>
                                    <h:panelGroup rendered="#{pi.billItem.bill.cancelled}" >
                                        <h:outputLabel style="color: red;" value="Cancelled : " />
                                        <h:outputLabel style="color: red;" rendered="#{pi.billItem.bill.cancelled}" value="#{pi.billItem.bill.cancelledBill.createdAt}" >
                                            <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm:ss a"/>
                                        </h:outputLabel>
                                    </h:panelGroup>
                                    <h:panelGroup rendered="#{pi.billItem.refunded eq true}" >
                                        <h:outputLabel style="color: red;" value="Refunded : " />
                                        <h:outputLabel style="color: red;" rendered="#{pi.billItem.refunded eq true}" value="#{pi.billItem.referanceBillItem.bill.createdAt}" >
                                            <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm:ss a"/>
                                        </h:outputLabel>
                                    </h:panelGroup>
                                </p:tooltip>
                            </p:column>


                            <p:column  headerText="Patient" styleClass="alignTop" >
                                <h:commandLink id="colPtDetail" rendered="#{webUserController.hasPrivilege('LabEditPatient') and pi.billItem.bill.approvedAnyTest}" 
                                               value="#{pi.billItem.bill.patient.person.nameWithTitle}, #{pi.billItem.bill.patient.age}" 
                                               action="/patient" target="_blank"
                                               style="text-decoration:underline;"
                                               styleClass="#{pi.billItem.bill.cancelled or pi.billItem.transRefund ? 'redtext':''}"
                                               >
                                    <f:setPropertyActionListener value="#{pi.billItem.bill.patient}" target="#{patientController.current}"/>
                                    <f:setPropertyActionListener value="#{pi.billItem.bill}" target="#{billController.bill}"/>
                                </h:commandLink>
                                <h:commandLink id="colPtDetail1" rendered="#{!pi.billItem.bill.approvedAnyTest}"
                                               value="#{pi.billItem.bill.patient.person.nameWithTitle}, #{pi.billItem.bill.patient.age}" 
                                               action="/patient" 
                                               style="text-decoration:underline;"
                                               styleClass="#{pi.billItem.bill.cancelled or pi.billItem.transRefund ? 'redtext':''}"
                                               >
                                    <f:setPropertyActionListener value="#{pi.billItem.bill.patient}" target="#{patientController.current}"/>
                                    <f:setPropertyActionListener value="#{pi.billItem.bill}" target="#{billController.bill}"/>
                                </h:commandLink>
                                <p:outputLabel rendered="#{pi.billItem.bill.approvedAnyTest and !webUserController.hasPrivilege('LabEditPatient')}"
                                               value="#{pi.billItem.bill.patient.person.nameWithTitle}, #{pi.billItem.bill.patient.age}" />
                                <p:tooltip for="colPtDetail"  >
                                    <h:outputLabel value="Name : #{pi.billItem.bill.patient.person.nameWithTitle}" >
                                    </h:outputLabel>
                                    <br/>
                                    <h:outputLabel value="Age : #{pi.billItem.bill.patient.age},&nbsp;#{pi.billItem.bill.patient.person.sex}," >
                                    </h:outputLabel>
                                    <br/>
                                    <h:outputLabel value="Phone : #{pi.billItem.bill.patient.person.phone}" >
                                    </h:outputLabel>
                                </p:tooltip>
                            </p:column>

                            <p:column headerText="Investigation"  styleClass="alignTop" >
                                <h:outputLabel value="#{pi.billItem.item.name}" >
                                </h:outputLabel>

                            </p:column>

                            <p:column headerText="Billed for"  styleClass="alignTop">
                                <h:outputLabel value="#{pi.billItem.bill.toDepartment.name}" >
                                </h:outputLabel>
                            </p:column>

                            <p:column  headerText="Actions"  styleClass="alignTop noBorderGrid"  >
                                <h:panelGrid columns="5" style="border: none;" styleClass="noBorderGrid"
                                             rendered="true" >
                                    <p:commandLink process="btnSample" update="tblBills"
                                                   disabled="#{pi.collected or pi.billItem.bill.cancelled}"   
                                                   id="btnSample" 
                                                   action="#{patientInvestigationController.markAsSampled}" >
                                        <f:setPropertyActionListener value="#{pi}" target="#{patientInvestigationController.current}"></f:setPropertyActionListener>
                                        <p:graphicImage library="image" styleClass="standedicon" name="bleed.png" ></p:graphicImage>
                                    </p:commandLink>
                                    <p:commandLink process="btnReceive" update="tblBills"  disabled="#{!pi.received ne true and !webUserController.hasPrivilege('LabSampleReceiving')}"   id="btnReceive"  action="#{patientInvestigationController.markAsReceived()}" >
                                        <f:setPropertyActionListener value="#{pi}" target="#{patientInvestigationController.current}"></f:setPropertyActionListener>
                                        <p:graphicImage library="image" styleClass="standedicon" name="receive.jpeg" ></p:graphicImage>
                                    </p:commandLink>
                                    <p:commandLink ajax="false" id="btnPrintWorksheets" action="/lab/single_worksheet" disabled="#{pi.billItem.bill.cancelled}">
                                        <f:setPropertyActionListener value="#{pi}" target="#{patientInvestigationController.current}" ></f:setPropertyActionListener>
                                        <p:graphicImage library="image" styleClass="standedicon" name="worksheet.gif" ></p:graphicImage>
                                    </p:commandLink>
                                    <p:commandLink id="btnNewReport" ajax="false" 
                                                   disabled="#{!pi.received}" 
                                                   action="/lab/patient_report"  
                                                   actionListener="#{patientReportController.createNewReport(pi)}" >
                                        <f:setPropertyActionListener value="#{pi}" target="#{patientReportController.currentPtIx}"></f:setPropertyActionListener>
                                        <p:graphicImage library="image" styleClass="standedicon" name="data_entry.png" ></p:graphicImage>
                                    </p:commandLink>
                                    <h:panelGroup >
                                        <p:commandLink process="btnSms" update="tblBills"  
                                                       id="btnSms" 
                                                       rendered="#{!pi.billItem.bill.smsed}"
                                                       disabled="#{pi.billItem.bill.cancelled or pi.billItem.transRefund or pi.patientReports.size() eq 0}"
                                                       action="#{patientInvestigationController.sendSms()}" >
                                            <f:setPropertyActionListener value="#{pi}" target="#{patientInvestigationController.current}"></f:setPropertyActionListener>
                                            <p:graphicImage library="image" styleClass="standedicon" name="sms.jpg" ></p:graphicImage>
                                        </p:commandLink>
                                        <p:commandLink process="btnSmsSent" update="tblBills"  
                                                       id="btnSmsSent"
                                                       rendered="#{pi.billItem.bill.smsed}"
                                                       disabled="#{pi.billItem.bill.cancelled or pi.billItem.transRefund or pi.patientReports.size() eq 0}"
                                                       action="#{patientInvestigationController.sendSms()}" >
                                            <f:setPropertyActionListener value="#{pi}" target="#{patientInvestigationController.current}"></f:setPropertyActionListener>
                                            <p:graphicImage library="image" styleClass="standedicon" name="smssent.png" ></p:graphicImage>
                                        </p:commandLink>

                                    </h:panelGroup>
                                </h:panelGrid>

                                <p:tooltip for="btnReceive"  >
                                    <h:outputLabel value="Receive Sample" rendered="#{!pi.received}" ></h:outputLabel>
                                    <h:outputLabel value="Sample received at " rendered="#{pi.received}" ></h:outputLabel>
                                    <h:outputLabel value="#{pi.receivedAt}" rendered="#{pi.received}" >
                                        <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                    </h:outputLabel>
                                </p:tooltip>

                                <p:tooltip for="btnSample" value="Collect Sample" >
                                    <h:outputLabel value="Collect Sample" rendered="#{!pi.collected }" ></h:outputLabel>
                                    <h:outputLabel value="Sampled at " rendered="#{pi.collected}" ></h:outputLabel>
                                    <h:outputLabel value="#{pi.sampledAt}" rendered="#{pi.collected}" >
                                        <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                    </h:outputLabel>
                                </p:tooltip>

                                <p:tooltip for="btnNewReport" value="New Report" ></p:tooltip>

                                <p:tooltip for="btnPrintWorksheets" value="Print worksheet" ></p:tooltip>


                                <table >
                                    <ui:repeat value="#{itemForItemController.getItemsForParentItem(pi.investigation)}" var="ifi"  >
                                        <tr>
                                            <td>
                                                <p:commandLink disabled="#{!pi.received}" value="New #{ifi.name}" actionListener="#{patientReportController.enterNewReportFormat(pi, ifi)}" action="/lab/patient_report" 
                                                               ajax="false">
                                                </p:commandLink>
                                            </td>
                                        </tr>
                                    </ui:repeat>
                                </table>


                            </p:column>

                            <p:column headerText="Data-Entered Reports"  styleClass="alignTop noBorderGrid" >
                                
                                
                                
                                <p:dataTable value="#{patientReportController.patientReports(pi)}" var="dpr" emptyMessage="" 
                                             rowStyleClass="#{dpr.retired eq true ? 'noDisplayRow':''}" 
                                             styleClass="noBorderGrid summeryBorder noBorder" >
                                    <p:column >
                                        <p:commandLink id="cmdOldReport" style="text-decoration:underline;" 
                                                       value="#{dpr.item.name}" 
                                                       ajax="false" action="/lab/patient_report">
                                            <f:setPropertyActionListener value="#{dpr}" target="#{patientReportController.currentPatientReport}"></f:setPropertyActionListener>
                                        </p:commandLink>
                                        <h:panelGrid columns="5" styleClass="noBorderGrid" >
                                            <p:commandLink id="cmdOldReportEdit" ajax="false" action="/lab/patient_report">
                                                <f:setPropertyActionListener value="#{dpr}" target="#{patientReportController.currentPatientReport}"></f:setPropertyActionListener>
                                                <p:graphicImage library="image" styleClass="standedicon" name="data_entry.png" ></p:graphicImage>
                                            </p:commandLink>

                                            <p:commandLink id="cmdOldReportPrint" ajax="false" action="/lab/patient_report_print">
                                                <p:graphicImage library="image" name="print.jpg" styleClass="standedicon" ></p:graphicImage>
                                                <f:setPropertyActionListener value="#{dpr}" target="#{patientReportController.currentPatientReport}"></f:setPropertyActionListener>
                                            </p:commandLink>

                                            <p:graphicImage id="imgApproved" library="image" name="approved_icon.png" 
                                                            style="height: 30px; width: 30px; #{dpr.approved ? '':'opacity: 0.1;'}" ></p:graphicImage>
                                            <p:graphicImage id="imgPrinted" library="image" name="print_ok.png" 
                                                            style="height: 30px; width: 30px;#{dpr.printed ? '':'opacity: 0.1;'}" ></p:graphicImage>



                                            <p:commandLink ajax="false"  
                                                           id="btnEmail" 
                                                           disabled="#{pi.billItem.bill.cancelled or pi.billItem.transRefund}"
                                                           action="#{patientReportController.sendEmail()}" >
                                                <f:setPropertyActionListener value="#{dpr}" target="#{patientReportController.currentPatientReport}"></f:setPropertyActionListener>
                                                <p:graphicImage library="image" styleClass="standedicon" name="email.jpg" ></p:graphicImage>
                                            </p:commandLink>

                                        </h:panelGrid>


                                        <p:tooltip for="imgApproved" >
                                            <h:outputLabel value="Approved" styleClass="#{dpr.approved ? 'greentext':'yellowtext'}" >
                                            </h:outputLabel>
                                        </p:tooltip>
                                        <p:tooltip for="imgPrinted" >
                                            <h:outputLabel value="Printed" styleClass="#{dpr.approved ? 'greentext':'yellowtext'}" >
                                            </h:outputLabel>
                                        </p:tooltip>
                                        <p:tooltip for="cmdOldReportPrint" >
                                            <h:outputLabel value="To Print" styleClass="#{dpr.approved ? 'greentext':'yellowtext'}" >
                                            </h:outputLabel>
                                        </p:tooltip>
                                        <p:tooltip for="cmdOldReport cmdOldReportEdit" >
                                            <h:outputLabel value="To Edit, Approve or Print" styleClass="#{dpr.approved ? 'greentext':'yellowtext'}" >
                                            </h:outputLabel>
                                            <br/>
                                            <h:outputLabel value="#{dpr.item.name}" styleClass="#{dpr.approved ? 'greentext':'yellowtext'}" >
                                            </h:outputLabel>
                                            <br/>
                                            <h:outputLabel value="Data enterd at " ></h:outputLabel>
                                            <h:outputLabel value="#{dpr.dataEntryAt}" >
                                                <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                            </h:outputLabel>
                                            <br/>
                                            <h:outputLabel value="Approved at " rendered="#{dpr.approved}" ></h:outputLabel>
                                            <h:outputLabel value="#{dpr.approveAt}"  rendered="#{dpr.approved}" >
                                                <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                            </h:outputLabel>
                                            <br/>
                                            <h:outputLabel value="Printed at "  rendered="#{dpr.printed}" ></h:outputLabel>
                                            <h:outputLabel value="#{dpr.printingAt}"  rendered="#{dpr.printed}" >
                                                <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm a"/>
                                            </h:outputLabel>
                                        </p:tooltip>


                                    </p:column>
                                </p:dataTable>


                            </p:column>

                        </p:dataTable>

                    </p:panel>

                </h:form>
            </ui:define>


        </ui:composition>

    </h:body>
</html>
